import path from 'path'
import File from '../util/File.js'
import { Router } from 'express'
import User from '../verify/User.js'
import UserDao from '../dao/UserDao.js'

let router = Router()

router.route('/api/user')
    .post(async (req, res) => {
        let user = User.insertObj(req.body)
        if (!user) return res.end(JSON.stringify({ code: 500, msg: '参数有误' }))
        
        let result = await UserDao.insert(user)
        if (result instanceof Error) return res.end(JSON.stringify({ code: 500, msg: '数据库操作失败' }))

        //删除所有临时文件
        File.delete(req.session.head_imgs, [ user.head_img ])
        req.session.head_imgs = []

        return res.end(JSON.stringify({ code: 200 }))
    })
    .delete(async (req, res) => {
        let user = User.deleteObj(req.body)
        if (!user) return res.end(JSON.stringify({ code: 500, msg: '参数有误' }))

        let result = await UserDao.delete(user)
        if (result instanceof Error) return res.end(JSON.stringify({ code: 500, msg: '数据库操作失败' }))

        return res.end(JSON.stringify({ code: 200 }))
    })
    .put(async (req, res) => {
        let user = User.updateObj(req.body)
        if (!user) return res.end(JSON.stringify({ code: 500, msg: '参数有误' }))

        //获取用户之前的头像地址
        let { head_img } = await UserDao.selectById(user.id)

        let result = await UserDao.update(user)
        if (result instanceof Error) return res.end(JSON.stringify({ code: 500, msg: '数据库操作失败' }))

        //删除所有临时文件
        let urls = head_img.split('/')
        let old_url = path.join(path.resolve(), '/public/assets', 'img', urls[urls.length - 1])

        req.session.head_imgs.push(old_url)

        File.delete(req.session.head_imgs, [ user.head_img ])
        req.session.head_imgs = []

        return res.end(JSON.stringify({ code: 200 }))
    })

router
    .get('/api/user/:id', async (req, res) => {
        let result = await UserDao.selectById(req.params.id)
        if (result instanceof Error) return res.end(JSON.stringify({ code: 500, msg: '数据库操作失败' }))

        return res.end(JSON.stringify({ code: 200, data: result }))
    })
    .get('/api/userByPage/:pageIndex/:pageSize', async (req, res) => {
        let { pageIndex, pageSize } = req.params

        let result = await UserDao.selectByLimit((pageIndex - 1) * pageSize, pageSize)
        if (result instanceof Error) return res.end(JSON.stringify({ code: 500, msg: '数据库操作失败' }))

        return res.end(JSON.stringify({ code: 200, data: result }))
    })
    .get('/api/userTotal', async (req, res) => {
        let result = await UserDao.selectCount()
        if (result instanceof Error) return res.end(JSON.stringify({ code: 500, msg: '数据库操作失败' }))

        return res.end(JSON.stringify({ code: 200, data: result }))
    })


    


router.route('/api/fan')
    .post(async (req, res) => {
        let { user_id, fan_id } = req.body
        if (!user_id || !fan_id) return res.end(JSON.stringify({ code: 500, msg: '参数有误' }))

        let result = await UserDao.insertFan(user_id, fan_id)
        if (result instanceof Error) return res.end(JSON.stringify({ code: 500, msg: '数据库操作失败' }))

        return res.end(JSON.stringify({ code: 200 }))
    })
    .delete(async (req, res) => {
        let { user_id, fan_id } = req.body
        if (!user_id || !fan_id) return res.end(JSON.stringify({ code: 500, msg: '参数有误' }))

        let result = await UserDao.deleteFan(user_id, fan_id)
        if (result instanceof Error) return res.end(JSON.stringify({ code: 500, msg: '数据库操作失败' }))

        return res.end(JSON.stringify({ code: 200 }))
    })

router
    .get('/api/follow/:id', async (req, res) => {
        let result = await UserDao.selectFollow(req.params.id)
        if (result instanceof Error) return res.end(JSON.stringify({ code: 500, msg: '数据库操作失败' }))

        return res.end(JSON.stringify({ code: 200, data: result }))
    })
    .get('/api/fan/:id', async (req, res) => {
        let result = await UserDao.selectFan(req.params.id)
        if (result instanceof Error) return res.end(JSON.stringify({ code: 500, msg: '数据库操作失败' }))

        return res.end(JSON.stringify({ code: 200, data: result }))
    })

export default router